package com.apes.framework.jpa.generator.model;

/**
 * 功能：日期序列
 *
 * @author xuliang
 * @create 2018-02-04 14:45
 */

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;

import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.outbox.annotation.OutBoxExclude;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;

@OutBoxExclude
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = SequenceDateRange.CONST_TABLE_NAME, indexes = {@Index(name = "ir_sequence_date_sequence", columnList = "sequence_id,date_from,date_to")})
public class SequenceDateRange extends Model {
    public static final String CONST_TABLE_NAME = "ir_sequence_date_range";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "日期序列")
    private long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "sequence_id", referencedColumnName = "id", nullable = false)
    private Sequence sequence;  //主序列

    @Column(name = "number_next", nullable = false)
    private int numberNext = 1; //下一号码

    @Temporal(TemporalType.DATE)
    @Column(name = "date_from", nullable = false)
    private Date dateFrom;  //从

    @Temporal(TemporalType.DATE)
    @Column(name = "date_to", nullable = false)
    private Date dateTo;  //至


    public void addNumberNext() {
        this.numberNext += this.getSequence().getNumberIncrement();
    }

}
